package com.atguigu.gmall.realtime.common.util;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 分词工具类
 */
public class IkUtil {
    public static List<String> splitWord(String keyword){

        ArrayList<String> words = new ArrayList<>();
        StringReader reader = new StringReader(keyword);
        IKSegmenter segmenter = new IKSegmenter(reader, true);
        try {
            Lexeme next;
            while ((next = segmenter.next()) != null){
                String word = next.getLexemeText();
                words.add(word);
            }
            // System.out.println("words: " + words);
            return words;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("分词失败...");
        }
    }
}
